import { NextRequest, NextResponse } from 'next/server' import { Drive_stat } from '@/lib/drive_server' // GET /download/[...path] - Download file by path (redirects to blob endpoint) export async function GET( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { try { const { path } = await params // Reconstruct the full path const fullPath = '/' + path.join('/') // Get filename from path for the download const filename = path[path.length - 1] || 'download' // Get blob ID using Drive_stat const blobId = await Drive_stat(fullPath) // Redirect to blob endpoint with filename - preserve original host // Use X-Forwarded-Host or Host header to get the correct host const forwardedHost = request.headers.get('x-forwarded-host') const host = forwardedHost || request.headers.get('host') || new URL(request.url).host const protocol = request.headers.get('x-forwarded-proto') || (new URL(request.url).protocol.replace(':', '')) const redirectUrl = `${protocol}://${host}/blob/${blobId}?filename=${encodeURIComponent(filename)}` return NextResponse.redirect(redirectUrl) } catch (error) { console.error('Download error:', error) return new NextResponse( error instanceof Error ? error.message : 'File not found', { status: 404 } ) } }